package com.faxsun.web.endpoint;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.faxsun.core.persist.entity.WechatMsgPO;
import com.faxsun.core.service.WechatService;
import com.faxsun.web.utils.WechatApiUtils;

@Controller
@RequestMapping("/api/v1")
public class WechatMsgEndpoint {

    private static final Logger LOG = LoggerFactory.getLogger(WechatMsgEndpoint.class);

    public static final String TEXT = "text";
    public static final String IMAGE = "image";
    public static final String VIDEO = "video";

    @Autowired
    WechatService wechatService;

    @Autowired
    WechatApiUtils wechatApiUtils;

    private static int turingOffset = 6;
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static Date turingDate = null;

    static {
        try {
            turingDate = sdf.parse("2015-06-08 19:30:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/wechatSave", method = RequestMethod.GET)
    @ResponseBody
    @Transactional
    public String save(@RequestParam("Topic") String topic, @RequestParam("FromUserName") String from,
            @RequestParam(value = "Content", required = false) String content,
            @RequestParam("CreateTime") String create, @RequestParam("MsgType") String msgType,
            @RequestParam("MsgId") String msgId, @RequestParam(value = "PicUrl", required = false) String picUrl,
            @RequestParam(value = "VideoUrl", required = false) String videoUrl) {

        try {
            WechatMsgPO po = new WechatMsgPO();

            if (topic.equals("turingphone2015")) {
                if (msgType.equals(IMAGE)) {
                    Calendar cal = Calendar.getInstance();
                    cal.setTime(turingDate);
                    cal.add(Calendar.MINUTE, turingOffset);// advance
                                                           // turingOffset
                                                           // minutes
                    turingDate = cal.getTime();
                }
                po.setCreateTime(turingDate);
            } else {
                po.setCreateTime(new Date(Long.parseLong(create) * 1000L));
            }

            po.setType(msgType);
            po.setId(msgId);
            po.setFromUserName("范客直播君@" + topic.toUpperCase());
            po.setTopic(topic);

            if (msgType.equals(TEXT)) {
                po.setContent(content);
            } else if (msgType.equals(IMAGE)) {
                po.setContent(picUrl);
            } else if (msgType.contains(VIDEO)) {
                po.setContent(videoUrl);
            }

            wechatService.save(po);
        } catch (Exception e) {
            LOG.error("", e);
        }
        return "";

    }

    @RequestMapping(value = "/wechatJsapiSign", method = RequestMethod.GET)
    @ResponseBody
    public String wechatJsapiSign(@RequestParam final String nonceStr, @RequestParam final String timestamp,
            @RequestParam final String url) {
        return wechatApiUtils.getJsapiSign(nonceStr, timestamp, url);
    }
}
